import { getToken, removeToken, setToken } from '@/utils/auth'
import { getUserInfoAPI, loginAPI } from '@/api'
export default {
  state: {
    token: getToken(),
    UserInfo: {}
  },
  mutations: {
    setToken(state, preload) {
      state.token = preload
      setToken(preload)
    },
    removeToken(state) {
      state.token = null
      removeToken()
    },
    setUserInfo(state, preload) {
      state.UserInfo = preload
    }
  },
  actions: {
    async   login(store, data) {
      const token = await loginAPI(data)
      store.commit('setToken', token)
    },
    async getUserInfo(store) {
      store.commit('setUserInfo', await getUserInfoAPI())
    },
    // logout登出
    logout(store) {
      store.commit('removeToken')
      store.commit('setUserInfo', {})
    }
  },
  namespaced: true
}
